<template>
  <div class="app-container">

<!--    <el-upload
      class="upload-demo"
      ref="upload"
      action="https://jsonplaceholder.typicode.com/posts/"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :file-list="fileList"
      :auto-upload="false">
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>-->

    <el-form label-width="120px">

      <el-form-item label="信息描述">
        <el-tag type="info">excel 模板说明</el-tag>
        <el-tag>
          <i class="el-icon-download"/>
          <a href="https://atguigu-edu-1001.oss-cn-hangzhou.aliyuncs.com/2021/05/04/02b7d1d663b844c5a5610d859f942fb1-subject.xls">点击下载模板</a>
        </el-tag>
      </el-form-item>

      <el-form-item label="上传文件">
        <el-upload
          ref="upload"
          accept=".xls,.xlsx"
          multiple=true
          :action="BASE_API + '/edu/subject/upload'"
          :auto-upload="false"
          :on-change="handleChange"
          :on-remove="handleRemove"
          :on-success="handleSuccess"
          :on-error="handleError"
          :disabled="importBtnDisabled"
          :limit="3"
          >
          <el-button
            slot="trigger"
            size="small"
            type="primary">选取文件</el-button>
          <el-button
            :loading="loading"
            :disabled="uploadBtnDisabled"
            style="margin-left: 10px;"
            size="small"
            type="success"
            @click="submitUpload">上传到服务器</el-button>
          <div slot="tip" class="el-upload__tip">只能上传xls/xlsx文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>

    </el-form>

  </div>
</template>

<script>
export default {
  data() {
    return {
      fileList: [
        {
          name: 'food.jpeg',
          url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'},
        {
          name: 'food2.jpeg',
          url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
        }
      ],

      BASE_API: process.env.VUE_APP_BASE_API,
      OSS_PATH: process.env.VUE_APP_OSS_PATH,
      ACTION_URL: "BASE_API + '/edu/subject/upload'",
      ACCEPT_TYPE_XLS: "application/vnd.ms-excel",
      ACCEPT_TYPE_XLSX: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      FILES_LIMIT: 1,
      fileUploadBtnText: '上传到服务器',
      importBtnDisabled: false,
      uploadBtnDisabled: true,
      loading: false
    };
  },
  created() {
  },
  methods: {
    submitUpload() {
      this.importBtnDisabled = true
      this.loading = true
      this.$refs.upload.submit()
    },

    handleChange(file, fileList) {
      console.log(file)
      console.log(fileList)
      // alert("文件状态改变")
      this.checkFileList(file, fileList)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
      this.checkFileList(file, fileList)
    },
    handlePreview(response) {
      console.log(response)
    },
    handleSuccess(response) {
      this.importBtnDisabled =false
      this.loading = false
      const url = response.data.url
      console.log(response)
      console.log(url)

      this.$refs.upload.clearFiles()

      this.$message({
        type: "success",
        message: "上传课程信息文件成功"
      })

      this.$router.push({
        path: "/subject/tree"
      })
    },
    handleError(response) {
      console.log(response)
      this.importBtnDisabled =false
      this.loading = false
      this.$message({
        type: "error",
        message: "上传失败"
      })
    },
    checkFileList(file, fileList) {
      this.uploadBtnDisabled = (file == null)
      this.uploadBtnDisabled = (fileList.length <= 0)
    }
  }
}
</script>
